MEMCHECK Version 1.0 The Memcheck library contains various functions to help monitor heap usage and heap consistency in a real time mode for programs written in Microsoft C. To make these functions active you must code a call to the MemWatch function from within your program. Usage: void MemWatch(n); int n; This function call installs the heap routines and optionally causes a _heapchk( ) call to be invoked every n clock ticks. A value of -1 causes only the heap routines to be installed and not the periodic checking of the heap. Pressing right shift F1 will display the memcheck menu. Currently this will display as follows: (W)alk Heap (H)eap Statistics (R)eset Heap Heap status is : HEAPOK the line showing the heap status can have the following values : HEAPOK : The heap is not corrupted HEAPBADBEGIN : The start of heap ptr is missing or is invalid HEAPBADNODE : The heap is damaged any value other than HEAPOK means that your program has probably 'stamped' on memory somewhere. Walk Heap. This option allows you to walk through each entry in the heap. the display gives you the address of the block of memory, the size of the block of memory and whether it is in use or is free. If a block is in use then you should have in your program a char *ptr that equals the heap data address. A free block is the result of doing a free(ptr) call. This display also shows you the contents of this heap block. Presiing PGDN and PGUP allows you to scroll through this memory. Pressing 'N' or 'P' allows you to move through the heap. Heap Statistics. This option gives you a snapshot of how your program is utilising memory. It displays the following information: Used Blocks - The number of heap entrys that are flagged as being in use; i.e they are assigned to ptrs that have been malloc( ) but not free( ); Free Blocks - The number of heap entrys that were malloc( ) then free( ) but have not been reused. The address and size of the largest used block of memory. The address and size of the largest free block of memory. The amount of memory available to your program. This number is the value returned by _memavl( ); Reset Probe. This option allows you to modify how often the _heapchk( ) function is called. The number entered is the number of clock ticks that must pass before a call to _heapchk( ) is made. An entry of 0 disables this call. Approximately 18.2 clock ticks occur a second so an entry of 50 will cause the _heapchk( ) routine to be invoked nearly every 3 seconds. If the return from _heapchk( ) is bad then the memcheck menu, described above, will automatically appear with the relevant heap error message displayed. WARNING: It can happen that the _heapchk( ) call is made while your program is in the middle of a malloc( ) or free( ). Because some internal reorganisation of the heap is taking place the _heapchk( ) function will give a bad return causing the memcheck menu to be displayed. To disable all these functions code the following function call: MemCancel( ); After this function call shift-f1 will no longer display the memcheck menu and the heap probe will be disabled. I wrote these functions to enable me to discover how malloc( ) and free( ) worked. They don't work the way you think they would in terms of reusing blocks of memory etc. This Library has been tested using Microsoft C version 5.1 and 6.0. I have no idea if it will work with any other C compiler. This library only works with large model C programs and on 286 or better Machines. If you would like librarys for other memory models or 8088 processors please contact me at the address below ot via CIS 71507,1033. Steve Bridges 1391 Union street Manchester NH 03104 As usual you use these functions at your own risk. Example Program: below is an example of how to incorporate the functions in this library and how to compile and link your application: void Memwatch(int); // function prototype main( ) { MemWatch(20); // link in the routines with a call to // _heapcheck every 20 clock ticks (about // every 1.1 seconds) ...... // do some stuff MemCancel( ); // disables the routines. } Compile. cl /c /AL /G2 xxxxx.c Link. link xxxxx,,,memcheck;